package com.example.demo.controller;

import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;

/**
 * @author zhangbiao
 * @date 2020/6/25 0:04
 * @desc 描述
 */
public class BaseController {

    @Autowired
    HttpServletRequest request;

    /**
     * 请求里面参数变成实体类
     * @param clazz
     * @param <T>
     * @return
     */
    public <T> T getEntityFromRequest(Class<T> clazz){
        Enumeration<String> enumeration = request.getParameterNames();
        JSONObject params = new JSONObject();
        while (enumeration.hasMoreElements()){
            String paramKey = enumeration.nextElement();
            params.put(paramKey, request.getParameter(paramKey));
        }
        return JSONObject.parseObject(params.toJSONString(), clazz);
    }
}
